import { createContext, useReducer } from 'react'

import initData from '../../public/data.json'

export const DataContext = createContext()

const reduce = (state, action) => {
    switch (action.type) {
        case 'set':
            state = action.value
            return state
        default:
            return state
    }
}

export const DataReducer = props => {

    const [data, dispatch] = useReducer(reduce, initData.data)

    return (
        <DataContext.Provider value={{ data, dispatch }}>
            {props.children}
        </DataContext.Provider>
    )
}
